package com.xgry.bpm.api.base.createtable.service.impl;

import com.xgry.bpm.api.base.createtable.entity.CreateTable;
import com.xgry.bpm.api.base.createtable.mapper.CreateTableMapper;
import com.xgry.bpm.api.base.createtable.service.CreateTableService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.apache.http.HttpStatus;

import java.util.List;

/**
* <p>
    * CreateTable服务实现类
    * </p>
*
* @author adrian
* @since 2020-07-07
*/
@Service
public class CreateTableServiceImpl extends ServiceImpl<CreateTableMapper, CreateTable>implements CreateTableService {

    @Override
    public int deleteById(String id){
        if(StringUtils.isEmpty(id)){
            throw new IllegalArgumentException(String.valueOf(HttpStatus.SC_METHOD_FAILURE));
        }
        return baseMapper.deleteById(id);
    }

    @Override
    public CreateTable selectById(String id){
        if(StringUtils.isEmpty(id)){
            throw new IllegalArgumentException(String.valueOf(HttpStatus.SC_METHOD_FAILURE));
        }
        CreateTable data=baseMapper.selectById(id);
        return data;
    }

    @Override
    public List<CreateTable>selectAll(CreateTable record){
        QueryWrapper<CreateTable>queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(record);
        List<CreateTable>list=baseMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public IPage<CreateTable>selectPage(IPage<CreateTable> page , CreateTable record){
        // 查询条件
        QueryWrapper<CreateTable>queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(record);
        return baseMapper.selectPage(page,queryWrapper);
    }

    @Override
    public IPage<CreateTable>getPage(IPage<CreateTable>page,QueryWrapper<CreateTable>queryWrapper){
        return baseMapper.selectPage(page,queryWrapper);
    }

    @Override
    public List<CreateTable> findByTableName(String tableClassName) {
        return baseMapper.selectList(new QueryWrapper<CreateTable>(){{
            this.eq("tableName",tableClassName);
        }});
    }

    @Override
    public void deleteByTableId(Integer tableClassId) {
        List<CreateTable> tableList = baseMapper.selectList(new QueryWrapper<CreateTable>() {{
            this.eq("tableClassId", tableClassId);
        }});
        for (CreateTable createTable : tableList) {
            baseMapper.deleteById(createTable.getTableId());
        }
    }
}